<!--
 * @Description: 精英任务列表
 * @Author: liyujie
 * @Date: 2021/05/10
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/05/10
-->
<template>
    <PageLayout title="任务管理">
        <template slot="header-right">
            <el-button
                size="medium"
                type="primary"
                icon="el-icon-plus"
                @click="handleRoute('eliteTaskEdit', {type: 'add'})"
            >
                新建任务
            </el-button>
        </template>
        <template slot="filter">
            <div class="filter-item">
                <p class="filter-item__title">
                    任务标题
                </p>
                <el-input
                    placeholder="请输入任务标题"
                    v-model="filterData.keyword"
                    suffix-icon="el-icon-search"
                    clearable
                    size="medium"
                    @keydown.enter.native="getTableData(1)"
                />
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    任务类型
                </p>
                <el-select
                    v-model="filterData.task_category"
                    filterable
                    placeholder="全部"
                    size="medium"
                    style="width: 152px"
                    @change="getTableData(1)"
                >
                    <el-option
                        v-for="item in typeList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    />
                </el-select>
            </div>
            <div class="filter-item">
                <p class="filter-item__title">
                    任务时间
                </p>
                <el-date-picker
                    v-model="dateTimeRange"
                    type="datetimerange"
                    range-separator="-"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :default-time="['00:00:00', '23:59:59']"
                    size="medium"
                    style="width: 360px"
                    value-format="timestamp"
                />
            </div>
            <div class="filter-item">
                <el-button
                    type="primary"
                    size="small"
                    @click="getTableData(1)"
                >
                    查询
                </el-button>
            </div>
        </template>
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            ref="table"
            class="data-table"
            :cell-style="{height: '87px'}"
            v-loading="tableLoading"
        >
            <el-table-column
                label="序号"
                align="center"
                width="80"
                type="index"
                fixed="left"
            />
            <el-table-column
                label="任务信息"
                width="180"
                fixed="left"
            >
                <template slot-scope="scope">
                    <div class="task-info">
                        <el-image :src="scope.row.task_logo"/>
                        <div
                            class="task-title"
                            style="margin-left: 12px"
                        >
                            {{ scope.row.task_title }}
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="任务类型"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.task_category === 3">普通任务</div>
                    <div v-if="scope.row.task_category === 2">寝室推广</div>
                    <div v-if="scope.row.task_category === 4">梯度任务</div>
                    <div v-if="scope.row.task_category === 5">多奖励任务</div>
                </template>
            </el-table-column>
            <el-table-column
                label="任务简介"
                width="150"
            >
                <template slot-scope="scope">
                    <el-tooltip
                        effect="dark"
                        :content="scope.row.task_intro"
                        placement="top"
                        :disabled="scope.row.task_intro.length < 20"
                    >
                        <div
                            class="task-info"
                            style="font-weight: 400"
                        >
                            <div class="task-title">
                                {{ scope.row.task_intro }}
                            </div>
                        </div>
                    </el-tooltip>
                </template>
            </el-table-column>
            <el-table-column
                label="任务图片"
                width="140"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.task_images">
                        <el-image
                            :src="scope.row.task_images.split(',')[0]"
                            style="width: 120px;height: 50px;cursor: pointer"
                            @click="fnShowPreview(scope.row.task_images.split(','))"
                        />
                    </div>
                    <div v-else>--</div>
                </template>
            </el-table-column>
            <el-table-column
                label="奖励设置"
                align="center"
            >
                <template slot-scope="scope">
                    <el-popover
                        placement="bottom"
                        width="640"
                        trigger="click"
                        v-if="scope.row.task_category === 4"
                    >
                        <div style="text-align: center;margin: 12px 0 24px;font-size: 16px">查看奖励设置</div>
                        <el-table :data="scope.row.award_config">
                            <el-table-column
                                width="150"
                                label="梯度等级"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    梯度{{ scope.$index + 1 }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                property="require_submit_count"
                                label="要求数量"
                                align="center"
                            />
                            <el-table-column
                                label="奖励类型"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.type === 1 ? '现金奖励' : '0元购学生券' }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="奖励金额/学生券数量"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.total_num }}{{ scope.row.type === 1 ? '元' : '张' }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="用户端显示金额"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    <div v-if="scope.row.type === 1">{{ scope.row.num }}元</div>
                                    <div v-else>--</div>
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-button
                            slot="reference"
                            type="text"
                        >查看设置</el-button>
                    </el-popover>
                    <el-popover
                        placement="bottom"
                        width="800"
                        trigger="click"
                        v-if="scope.row.task_category === 5"
                    >
                        <div style="text-align: center;margin: 12px 0 24px;font-size: 16px">查看奖励设置</div>
                        <el-table :data="scope.row.award_config">
                            <el-table-column
                                width="150"
                                :label="scope.row.award_config[0].type === 1 ? '主任务' : '子任务'"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.require_submit_count_type === 1 ? '主任务' : '子任务' }}{{ scope.$index + 1 }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                width="100"
                                property="name"
                                label="截止时间"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    {{ Number(scope.row.require_second) / 3600 }}小时
                                </template>
                            </el-table-column>
                            <el-table-column
                                property="require_submit_count"
                                :label="scope.row.award_config[0].type === 1 ? '主任务数量' : '子任务数量'"
                                align="center"
                            />
                            <el-table-column
                                label="奖励类型"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.type === 1 ? '现金奖励' : '0元购学生券' }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="奖励金额/学生券数量"
                                align="center"
                                width="130"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.total_num }}{{ scope.row.type === 1 ? '元' : '张' }}
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="用户端显示金额"
                                align="center"
                                width="130"
                            >
                                <template slot-scope="scope">
                                    <div v-if="scope.row.type === 1">{{ scope.row.num }}元</div>
                                    <div v-else>--</div>
                                </template>
                            </el-table-column>
                            <el-table-column
                                label="前置条件"
                                align="center"
                            >
                                <template slot-scope="scope">
                                    {{ scope.row.require_pre_type === 1 ? '上一任务成功' : '上一任务结束' }}
                                </template>
                            </el-table-column>
                        </el-table>
                        <el-button
                            slot="reference"
                            type="text"
                        >查看设置</el-button>
                    </el-popover>
                    <div v-if="scope.row.task_category === 3">
                        {{ scope.row.award_config }}元
                    </div>
                    <div v-if="scope.row.task_category === 2">--</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="accept_count"
                label="参与人数"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                label="任务时间"
                width="200"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <template v-if="scope.row.apply_start_time !== -1">
                        <div>起始：{{ scope.row.apply_start_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                        <div>截止：{{ scope.row.apply_end_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                    </template>
                    <template v-else>
                        <div>起始：{{ scope.row.start_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                        <div>截止：{{ scope.row.end_time * 1000 | dateFormatter('yyyy-MM-dd HH:mm:ss') }}</div>
                    </template>
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                align="center"
                :width="120"
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.task_sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="editSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="任务状态"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot', scope.row.status === 1 ? 'success' : 'normal']"/>
                        <div>{{ scope.row.status | statusFilter }}</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                label="是否反馈"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    {{ scope.row.is_need_submit === 2 ? '是' : '否' }}
                </template>
            </el-table-column>
            <el-table-column
                label="上架状态"
                show-overflow-tooltip
                align="center"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.task_status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="setTaskStatus($event,scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="left"
                width="180"
                fixed="right"
            >
                <template slot-scope="scope">
                    <div class="option-btn-group">
                        <el-button
                            type="text"
                            size="medium"
                            @click="handleRoute('eliteTaskEdit', {task_no: scope.row.task_no, type: 'edit', status: scope.row.status})"
                            v-if="scope.row.status !== 2"
                        >
                            编辑
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="toReceiveList(scope.row)"
                        >
                            查看报名
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            v-if="scope.row.status === 2"
                            @click="handleDelete(scope.row)"
                        >
                            删除
                        </el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <!-- /分页 -->
        <template slot="pagination">
            <div/>
            <el-pagination
                layout="total, prev, pager, next, jumper"
                background
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </template>
        <!-- 图片预览 -->
        <img-preview ref="preview"/>
    </PageLayout>
</template>

<script>
import { back } from '@/mixin/back';
import ImgPreview from "@/components/common/img-preview";

export default {
    name: 'ShopTaskVerifyList',
    mixins: [back],
    components: {ImgPreview},
    data() {
        return {
            filterData: {
                // 任务标题
                keyword: '',
                // 任务类型
                task_category: 99,
                // 任务时间
                start_time: '',
                end_time: ''
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            dateTimeRange: [],
            // 任务类型
            typeList: [
                {label: '全部', value: 99},
                {label: '普通任务', value: 3},
                {label: '寝室推广', value: 2},
                {label: '梯度任务', value: 4},
                {label: '多奖励任务', value: 5},
            ],
        };
    },
    computed: {},
    watch: {
        dateTimeRange(value) {
            this.filterData.start_time = value ? value[0] / 1000 : '';
            this.filterData.end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },
    filters: {
        statusFilter(val) {
            switch (val) {
                case 0:
                    return '未开始';
                case 1:
                    return '进行中';
                case 2:
                    return '已结束';
                default:
                    return '--';
            }
        }
    },
    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '平台标准任务'}, {title: '任务管理'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {
        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/task_stuagent%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         * @param index
         */
        fnShowPreview(images, index) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images, index);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * 排序修改
         */
        editSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入',
                inputValue: row.task_sort,
                closeOnClickModal: false
            }).then(({ value }) => {
                let params = {
                    task_no: row.task_no,
                    task_sort: value
                };

                this.$post('/student/task_stuagent%5Cset_sort', params ,resp=>{
                    if (resp.code === 1) {
                        this.getTableData();
                        this.$notify({
                            title: '提示',
                            type: 'success',
                            message: '设置成功'
                        });
                    } else {
                        this.$notify({
                            title: '提示',
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 设置任务状态
         * @param val
         * @param row
         */
        setTaskStatus(val, row) {
            let params = {
                task_no: row.task_no,
                task_status: val
            };

            this.$post('/student/task_stuagent%5Cset_status', params, resp => {
                if(resp.code === 1) {
                    this.$notify({
                        title: '成功',
                        message: '设置成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.getTableData();
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 删除
         * @param row
         */
        handleDelete(row) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            let params = {
                task_no: row.task_no
            };

            this.$confirm('是否要删除此任务?','提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/task_stuagent%5Cdelete', params, resp => {
                    if (resp.code === 1) {

                        this.$message.success('删除成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 跳转领取列表
         * @param row
         */
        toReceiveList(row) {
            const typeMap = new Map([
                [2, 'dorm'],
                [3, 'normal'],
                [4, 'gradient'],
                [5, 'multiple'],
            ]);

            this.handleRoute('eliteTaskReceiveList', {
                task_no: row.task_no,
                active: typeMap.get(row.task_category),
                submit_type: row.task_event === 'fans_invite' ? 101 : 2
            });
        }
    }
};
</script>

<style lang="less" scoped>
.task-info {
    display: flex;
    align-items: center;
    font-size: 14px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #333333;
    line-height: 20px;
    /deep/ .el-image {
        flex-shrink: 0;
        img {
            width: 50px;
            height: 50px;
        }
    }
    .task-title {
        display: -webkit-box;
        -webkit-box-orient: vertical;
        overflow: hidden;
        /*! autoprefixer: off */
        -webkit-line-clamp: 2;
    }
}
/* 排序 */
.sort-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    .sort-spliter {
        width: 1px;
        height: 12px;
        background: rgba(235, 235, 235, 1);
        margin: 0 10px;
    }
    .btn-sort {
        font-size: 14px;
        line-height: 1;
        color: #ccc;
        &:hover {
            color: #20a0ff;
        }
    }
}
/deep/ .el-image__inner  {
    object-fit: cover;
}
/deep/ .el-table {
    .el-table__row.hover-row {
        td {
            background-color: #fff;
        }
    }
}
</style>
